package com.ant.collect.config;

import cn.dev33.satoken.interceptor.SaInterceptor;
import cn.dev33.satoken.stp.StpUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

/**
 * @author hzd
 * @version 1.0
 * @date 2022/3/16 9:15
 */
@Configuration
public class SaTokenConfig extends WebMvcConfigurationSupport {

    @Value("${file.upload-path}")
    private String uploadPath;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new SaInterceptor(handler -> {
            StpUtil.checkLogin();
        }))
                .addPathPatterns("/**") // 拦截所有路径
                .excludePathPatterns("/captcha/generate", "/login", "/login/**","/filer/**")
                // 排除不需要拦截的路径（如静态资源，可选）
                .excludePathPatterns("/static/**", "/favicon.ico","filer/**",
                        "/api/public/**",
                        "/login",
                        "/login/**",
                        "/loginOut",
                        "/api/login/**",
                        "/api/login/loginOut",
                        "/swagger-ui.html",
                        "/swagger-ui/**", // Swagger UI 静态资源
                        "/swagger-resources/**",
                        "/webjars/**",
                        "/doc.html",
                        "/swagger-ui/**",
                        "/v3/api-docs/**");
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        super.addResourceHandlers(registry);
        registry.addResourceHandler("swagger-ui.html")
                .addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");
        registry.addResourceHandler("/doc.html")
                .addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
        registry.addResourceHandler("/pic/**").addResourceLocations("file:///" + uploadPath);
    }

    /**
     * 跨域处理
     *
     * @param registry
     */
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        super.addCorsMappings(registry);
        registry.addMapping("/**").
                allowedOriginPatterns("*").// 设置允许跨域请求的域名
                //allowedOrigins("*"). // 将.allowedOrigins替换成.allowedOriginPatterns即可。
                allowedMethods("*"). //允许任何方法（post、get等）
                allowedHeaders("*"). //允许任何请求头
                allowCredentials(true). //带上cookie信
                maxAge(3600L);  // 跨域允许时间
        //  exposedHeaders(HttpHeaders.SET_COOKIE).maxAge(3600L); //maxAge(3600)表明在3600秒内，不需要再发送预检验请求，可以缓存该结果
    }

}
